﻿using RimWorld;
using System.Linq;
using UnityEngine;
using Verse;

namespace RimZombieGirl
{
    public class MapComponent_Curse : MapComponent
    {
        public float currentCurseValue = 0f;   // 当前诅咒值
        public float targetCurseValue = 0f;    // 目标诅咒值

        public const float DailyChange = 50f;   // 每天最多变动50
        public const float TicksPerDay = 60000f;
        public const float ChangePerTick = DailyChange / TicksPerDay;

        public int nextIncidentTick = 0; //触发事件tick
        public MapComponent_Curse(Map map) : base(map)
        {
        }

        public override void MapComponentTick()
        {
            base.MapComponentTick();

            if (currentCurseValue < targetCurseValue)
            {
                currentCurseValue += ChangePerTick;
                if (currentCurseValue > targetCurseValue)
                    currentCurseValue = targetCurseValue;
            }
            else if (currentCurseValue > targetCurseValue)
            {
                currentCurseValue -= ChangePerTick;
                if (currentCurseValue < targetCurseValue)
                    currentCurseValue = targetCurseValue;
            }
            //事件触发
            if (Find.TickManager.TicksGame >= nextIncidentTick)
            {
                TryFireCurseIncident();
            }
        }

        public override void ExposeData()
        {
            base.ExposeData();
            Scribe_Values.Look(ref currentCurseValue, "currentCurseValue", 0f);
            Scribe_Values.Look(ref targetCurseValue, "targetCurseValue", 0f);
            Scribe_Values.Look(ref nextIncidentTick, "nextIncidentTick", 0);
        }

        private void TryFireCurseIncident()
        {
            if (currentCurseValue <= 0f)
                return;

            //动态计算下次触发的间隔
            float meanDays = 10f * (500f / (currentCurseValue + 1f));
            int interval = Rand.RangeInclusive(
                (int)(meanDays * 0.5f * TicksPerDay),
                (int)(meanDays * 1.5f * TicksPerDay)
            );
            nextIncidentTick = Find.TickManager.TicksGame + interval;

            IncidentDef incident = null;

            var allDefs = DefDatabase<IncidentDef>.AllDefs
                .Where(d => d.Worker.CanFireNow(new IncidentParms { target = map }))
                .ToList();

            //触发邪祟事件的概率随着诅咒值提升而增加，到500时几乎100%
            float cursedChance = Mathf.Clamp01(currentCurseValue / 500f);

            if (Rand.Chance(cursedChance))
            {
                incident = allDefs.Where(d => d.tags != null && d.tags.Contains("RI_Cursed"))
                                  .InRandomOrder().FirstOrDefault();
            }

            //轮空
            if (incident == null)
            {
                return;
            }

            //执行事件
            if (incident != null)
            {
                IncidentParms parms = StorytellerUtility.DefaultParmsNow(incident.category, map);
                incident.Worker.TryExecute(parms);
            }
        }

        /// <summary>
        /// 外部调用：设置目标诅咒值
        /// </summary>
        public void SetTargetCurseValue(float value)
        {
            targetCurseValue = value;
        }
        /// <summary>
        /// 外部调用：增加目标诅咒值
        /// </summary>
        public void AddTargetCurseValue(float delta)
        {
            targetCurseValue += delta;
            if (targetCurseValue < 0f) targetCurseValue = 0f; // 不允许负值
        }
        ///<summary>
        ///外部调用：减少目标诅咒值
        /// </summary>
        public void ReduceTargetCurseValue(float delta)
        {
            targetCurseValue -= delta;
            if (targetCurseValue < 0f) targetCurseValue = 0f; // 不允许负值
        }
        /// <summary>
        /// 外部调用: 获取当前诅咒值
        /// </summary>
        public float GetCurrentCurseValue()
        {
            return currentCurseValue;
        }
    }
}
